package com.rulin.asp.business.errorcorrection.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rulin.asp.business.entity.Dictionary;
import com.rulin.asp.business.entity.ErrorQuestionParam;
import com.rulin.asp.business.entity.Page;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.RepeatQuestion;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesAttach;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesParam;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesResultRow;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuestion;
import com.rulin.asp.business.errorcorrection.entity.MyPostMessage;
import com.rulin.asp.business.errorcorrection.mapper.ErrorQuesAttachMapper;
import com.rulin.asp.business.mapper.DictionaryMapper;
import com.rulin.asp.business.mapper.QuestionMapper;
import com.rulin.asp.util.Consists;
import com.rulin.asp.util.postutil.PostMessageUtil;

@Service
public class ErrorCorrectionService {

	@Autowired
	private ErrorQuesAttachMapper errorQuesAttachMapper;
	
	@Autowired
	private QuestionMapper questionMapper;
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	private static final String BASE_URL = "http://118.31.42.108/asp";
	
	@Transactional
	public int saveErrorQues(ErrorQuestion bean){
		ErrorQuesAttach errorQuesAttach = bean.getErrorQuesAttach();
		Question question = bean.getQuestion();
		int i = questionMapper.updateByPrimaryKeySelective(question);
		if(i == 0){
			question.setQuesDiffLevel("1");
			question.setQuesTime(new Date());
			question.setQuesType(Consists.QUES_TYPE_0);
			question.setQuestionSource("4");
			question.setQuesClosely("0");
			question.setValid("0");
			questionMapper.insert(question);
			errorQuesAttach.setIsError(Consists.IS_ERROR_Y);
			errorQuesAttach.setIsRepeat(Consists.IS_REPEAT_N);
			errorQuesAttach.setIsRepeatAdmin(Consists.IS_REPEAT_ADMIN_N);
			errorQuesAttach.setIsReward(Consists.IS_REWARD_Y);
			errorQuesAttachMapper.insert(errorQuesAttach);
		}else{
			errorQuesAttachMapper.updateByPrimaryKeySelective(errorQuesAttach);
		}
		return i;
	}
	
	/**
	 * 获取纠错信息
	 * @param quesId
	 * @return
	 */
	public ErrorQuestion getErrorQuestion(String quesId){
		Question question = questionMapper.selectByPrimaryKey(quesId);
		if(question == null){
			return null;
		}
		List<String> quesPics = questionMapper.selectQuesPics(quesId);
		question.setQuesPics(quesPics);
		if(question.getReplyId() != null ) {
			List<String> replyPics = questionMapper.selectReplyPics(question.getReplyId());
			question.setReplyPics(replyPics);
		}
		ErrorQuesAttach errorQuesAttach = errorQuesAttachMapper.selectByPrimaryKey(quesId);
		ErrorQuestion errorQuestion = new ErrorQuestion();
		errorQuestion.setErrorQuesAttach(errorQuesAttach);
		errorQuestion.setQuestion(question);
		return errorQuestion;
	}
	
	/**
	 * 分页查询纠错问题
	 * @param param
	 * @return
	 */
	public Pair<Integer, List<ErrorQuesResultRow>> geErrortQuesListByParam(ErrorQuesParam param){
		Integer total = errorQuesAttachMapper.countErrorQuesByParam(param);
		Page page = param.getPage();
		page.setTotal(total);
		List<ErrorQuesResultRow> list = errorQuesAttachMapper.selectErrorQuesByParam(param);
		return Pair.of(total, list);
	}
	
	/**
	 * 根据书本和页码获取已存在的纠错问题
	 * @param param
	 * @return
	 */
	public List<RepeatQuestion> getRepeatQuestions(ErrorQuestionParam param){
		return questionMapper.selectRepeatQuestions(param);
	}
	
	/**
	 * 确认重复题目并注销
	 * @param quesId
	 */
	@Transactional
	public void confirmRepeat(String quesId){
		Question question = new Question();
		question.setQuesId(quesId);
		question.setValid("1");//注销
		questionMapper.updateByPrimaryKeySelective(question);
		
		ErrorQuesAttach errorQuesAttach = new ErrorQuesAttach();
		errorQuesAttach.setQuesId(quesId);
		errorQuesAttach.setIsRepeat(Consists.IS_REPEAT_Y);
		errorQuesAttachMapper.updateByPrimaryKeySelective(errorQuesAttach);
	}
	
	/**
	 * 确认提交
	 * @param quesId
	 */
	@Transactional
	public void confirmSubmit(String quesId){
		
		Question question = questionMapper.selectByPrimaryKey(quesId);
		String bookClass = question.getBookClass();
		if("1".equals(bookClass) || "3".equals(bookClass)){// 如果是执业医师类别 + 助理医师类别自动指派我们这边指定的答疑老师
			List<Dictionary> selectByModel = dictionaryMapper.selectByModel("yincheng_teacher");
			if(CollectionUtils.isNotEmpty(selectByModel)){
				Dictionary user = selectByModel.get(0);
				String userAccount = user.getCode();
				String userName = user.getValue();
				question.setClaimUserAccount(userAccount);
				question.setClaimUserName(userName);
				question.setQuesType(Consists.QUES_TYPE_3);
				question.setClaimTime(new Date());
				question.setQuesTime(new Date());
				questionMapper.updateByPrimaryKeySelective(question);
			}
		}
		
	}
	
	public void complete(String quesId){
		Question question = new Question();
		question.setQuesId(quesId);
		question.setQuesType(Consists.QUES_TYPE_6);
		questionMapper.updateByPrimaryKeySelective(question);
		
		MyPostMessage postMessage = errorQuesAttachMapper.selectPostMessageByQuesId(quesId);
		
		String id = postMessage.getQuesId();
		String receiveMailAccount = postMessage.getEmail();
		String receiveName = postMessage.getUserName();
		String bookName = postMessage.getBookSecClassName();
		Integer pageNum = postMessage.getPageNum();
		String title = "关于"+bookName+"第"+pageNum+"页面图书纠错的反馈";
		String postMessageStr = receiveName+"同学：</br>"+
				"&nbsp;&nbsp;&nbsp;&nbsp;您好！首先感谢您对贺银成系列图书的支持和对图书纠错活动的积极参与。您提交的关于"+bookName+"第"+pageNum+"页面图书纠错信息我们已经审核完毕，下面附上您所提交的图书纠错信息及我们审核的结果链接。</br>"+
				"<a href='"+BASE_URL+"/external/errorCorrection/openReadPage.html?quesId="+id+"'>点击查看具体回复信息</a></br>";
		try {
			PostMessageUtil.sendMessage(receiveMailAccount, receiveName, title, postMessageStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
